package com.liumuxian.exam.service.impl;


import com.liumuxian.exam.dao.QuestionDao;
import com.liumuxian.exam.pojo.Question;
import com.liumuxian.exam.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private IdWorker idWorker;

    public Page<Question> findSearch(Map whereMap, int page, int size) {
        Specification<Question> specification = createSpecification(whereMap);
        PageRequest pageRequest =  PageRequest.of(page-1, size);
        return questionDao.findAll(specification, pageRequest);
    }

    @Override
    @Transactional
    public void add(Question question){
        question.setId(idWorker.nextId()+"");
        question.setCreatedate(new Date());
        question.setState(1);
            questionDao.save(question);
    }


    @Override
    @Transactional
    public void addList(List<Question> question) {
        question.forEach( q -> {
            q.setId(idWorker.nextId()+"");
            q.setCreatedate(new Date());
            q.setState(1);
        });
        questionDao.saveAll(question);
    }
    /**
     * 查询全部列表
     * @return
     */
    public List<Question> findAll() {
        return questionDao.findAll();
    }

    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<Question> createSpecification(Map searchMap) {

        return new Specification<Question>() {

            @Override
            public Predicate toPredicate(Root<Question> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // ID
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                    predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 题目名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                    predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                //试题属性
                if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
                    predicateList.add(cb.like(root.get("type").as(String.class), "%"+(String)searchMap.get("type")+"%"));
                }
                // 状态
                if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
                    predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

}
